#ifndef TASKQUEUE_H
#define TASKQUEUE_H

#include "common.h"
#include "Protocol.h"
#include <memory>
#include <mutex>

class CTaskQueue
{
public:
    ~CTaskQueue();

    static CTaskQueue* GetInstance();
    unsigned int GetTransId();

    MsgPtr GetTask(uint64_t tranid);
    MsgPtr GetTask(uint64_t tranid, BasicDataPtr remote, uint64_t task_id);

    void AddTask(uint64_t tranid, MsgPtr data);
    void DelTask(uint64_t tranid);
    void RemoveTask(uint64_t tranid);
    bool FindTask(uint64_t tranid);

    void SetAcsStatus(bool bMain, int stauts);

    int GetSize();
    int GetAcsStatus(bool bMain);
    int GetAcsStatus(int &mainstatus, int &slavestatus); // 1:normal 2:abnormal 3:temporary

    bool IsAcsActive(bool bMain, int &mainstatus, int &slavestatus);

private:

    CTaskQueue();

    static CTaskQueue           *m_pInstance;
    
    std::mutex                  m_mutex;

    class CGarbo
    {
    public:
        ~CGarbo()
        {
            if(CTaskQueue::m_pInstance)
                delete CTaskQueue::m_pInstance;
        }
    };
    static CGarbo Garbo;
    unsigned int m_TransId;

    std::map<uint64_t, MsgPtr> m_TaskQueue;

    std::atomic<int> m_MainAcsStatus, m_SlaveAcsStatus;                                                 // -1未启动 0宕机 1等待 2活动 3备用 4失步 5单机

    int m_CmsRole;                                                                                      //网管角色: 1五合一主, 2五合一备, 3单机(默认), 4网管热备主, 
    int m_CmsRealRole;                                                                                  // 登录自治时 使用用户的角色 如果是2 则使用admin9当做root
};

#endif // TASKQUEUE_H
